package com.gbx.ximalayating.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.gbx.ximalayating.R;
import com.gbx.ximalayating.entity.ChildList;
import com.gbx.ximalayating.entity.ListData;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by baoxing on 2016/12/05.
 */
public class AnchorViewPagerAdapter extends RecyclerView.Adapter<AnchorViewPagerAdapter.ViewHolder> {
    private List<ListData> list;
    private Context context;

    public AnchorViewPagerAdapter(List<ListData> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.fragment_anchor_item,parent,false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.anchor_title_tv.setText(list.get(position).getTitle());
        List<ChildList> childLists = this.list.get(position).getList();
        if (childLists != null) {
            List<String>smallLogo = new ArrayList<>();
            List<String>nickname = new ArrayList<>();
            for (int i = 0; i < childLists.size(); i++) {
                smallLogo.add(childLists.get(i).getSmallLogo());
                nickname.add(childLists.get(i).getNickname());
            }
            Picasso.with(context).load(smallLogo.get(0)).into(holder.anchor_item_1_image);
            Picasso.with(context).load(smallLogo.get(1)).into(holder.anchor_item_2_image);
            Picasso.with(context).load(smallLogo.get(2)).into(holder.anchor_item_3_image);
            holder.anchor_item_1_tv.setText(nickname.get(0));
            holder.anchor_item_2_tv.setText(nickname.get(1));
            holder.anchor_item_3_tv.setText(nickname.get(2));
        }
    }
    public void addData(Collection<? extends ListData> collection) {
        list.addAll(collection);
        notifyItemRangeInserted(list.size(),collection.size());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView anchor_title_tv;
        private ImageView anchor_item_1_image;
        private ImageView anchor_item_2_image;
        private ImageView anchor_item_3_image;
        private TextView anchor_item_1_tv;
        private TextView anchor_item_2_tv;
        private TextView anchor_item_3_tv;
        public ViewHolder(View itemView) {
            super(itemView);
            anchor_title_tv = (TextView) itemView.findViewById(R.id.anchor_title_tv);
            anchor_item_1_image = (ImageView) itemView.findViewById(R.id.anchor_item_1_image);
            anchor_item_2_image = (ImageView) itemView.findViewById(R.id.anchor_item_2_image);
            anchor_item_3_image = (ImageView) itemView.findViewById(R.id.anchor_item_3_image);
            anchor_item_1_tv = (TextView) itemView.findViewById(R.id.anchor_item_1_tv);
            anchor_item_2_tv = (TextView) itemView.findViewById(R.id.anchor_item_2_tv);
            anchor_item_3_tv = (TextView) itemView.findViewById(R.id.anchor_item_3_tv);

        }
    }
}
